<template>
	<view class="">
		<!-- 状态栏 -->
		<page-status ></page-status>
		<!-- 头部 -->
		<goods-header></goods-header>
		<!-- 轮播图 -->
		<view class="swiper-box">
			<swiper circular="true" autoplay="true" @change="swiperChange">
				<swiper-item v-for="swiper in goodsData.swiperList" :key="swiper.id">
					<image :src="swiper.img"></image>
				</swiper-item>
			</swiper>
			<view class="indicator">{{currentSwiper+1}}/{{goodsData.swiperList.length}}</view>
		</view>
		<!-- 标题 价格 -->
		<view class="info-box goods-info">
			<view class="price">￥{{goodsInfo.price}}</view>
			<view class="title">
				{{goodsInfo.name}}
			</view>
		</view>
		<!-- 选择规格 -->
		<view class="info-box">
			<view class="row" @tap="spaceInfo.showSpace = true">
				<view class="text">选择</view>
				<view class="content">
					<view>选择规格：</view>
					<view class="sp">
						<view v-for="(item,index) in goodsData.spec" :key="index" :class="{'on':item==goodsInfo.spec}">
							{{item}}
						</view>
					</view>
				</view>
				<!-- 箭头 -->
				<view class="arrow">
					<view class="icon iconfont">&#xe6a3;</view>
				</view>
			</view>
		</view>
		<!-- 模态框 -->
		<popup-spec :goodsData="goodsData" :goodsInfo='goodsInfo' :spaceInfo='spaceInfo'></popup-spec>
		<!-- 评价 -->
		<view class="info-box comments">
			<view class="row">
				<view class="text">商品评价{{goodsData.comment.length}}</view>
				<view class="arrow" @tap="handleRatings">
					<view class="show">
						查看全部
						<view class="icon iconfont">&#xe6a3;</view>
					</view>
				</view>
			</view>
			<view class="comment" @tap="handleRatings">
				<view class="user-info">
					<view class="face">
						<image :src="goodsData.comment[0].face"></image>
					</view>
					<view class="username">{{goodsData.comment[0].username}}</view>
				</view>
				<view class="content">
					{{goodsData.comment[0].content}}
				</view>
			</view>
		</view>
		<!-- 详情 -->
		<view class="description">
			<view class="title">———— 商品详情 ————</view>
			<view class="content">
				<rich-text :nodes="goodsData.descriptionStr"></rich-text>
			</view>
		</view>
		<!-- 底部菜单 -->
		<view class="footer">
			<view class="icons">
				<view class="box">
					<view class="icon iconfont">&#xe7e0;</view>
					<view class="text">分享</view>
				</view>
				<view class="box" @tap="keep">
					<view class="icon iconfont" v-if="isKeep">&#xe64b;</view>
					<view class="icon iconfont" v-else>&#xe64c;</view>
					<view class="text">{{isKeep?'已':''}}收藏</view>
				</view>
			</view>
			<view class="btn">
				<view class="joinCart" @tap="joinCart">加入购物车</view>
				<view class="buy" @tap="buy">立即购买</view>
			</view>
		</view>
	</view>
</template>

<script>
	import goodsHeader from './goodsHeader.vue' 
	import interfaces from '../../utils/interfaces.js'
	import popupSpec from '../../components/popupSpec.vue'
	
	export default {
		data() {
			return {
				goodsInfo:{
					name:'',
					price:'',
					number:1,
					spec:''
			    },
				goodsData: {
					swiperList:[],
					spec:[],
					comment:[{face:'', username:'', content:''}],
					descriptionStr:''
				},
				currentSwiper: 0, //轮播图下标
				spaceInfo: {showSpace: false},
				isKeep:false //收藏
				
			}
		},
		components: {
			goodsHeader,
			popupSpec
		},
		onLoad(option) {
			// console.log(option)
			this.goodsInfo = JSON.parse(option.goodsInfo)
			
			// 初始化数据
			this.goodsInfo.number = 1
			this.goodsInfo.spec = 'XS'
			
			this.initData()
		},
		methods: {
			initData() {
				this.request({
					url: interfaces.getGoods,
					success: res => this.goodsData = res.data 
				})
			},
			swiperChange(event) {
				this.currentSwiper = event.detail.current
			},
			handleRatings() {
				// 本地存储
				uni.setStorageSync('comments', this.goodsData.comment)
				// 页面跳转
				uni.navigateTo({
					url: './ratings'
				})
			},
			keep() {
				this.isKeep = !this.isKeep
				if(this.isKeep) {
					uni.showToast({
						icon:'success',
						title:'已收藏'
					})
				}
			},
			joinCart(){ // 加入购物车
				//1.本地存储中获取
				uni.getStorage({
					key: "goodsList",
					success: res => {
						// 获取数据
						let goodsList = res.data
												
						// 判断是否为同种商品
						let isExist = false
						
						goodsList.forEach(goods => {
							if (goods.goods_id == this.goodsInfo.goods_id && goods.spec == this.goodsInfo.spec) {
								// 如果存在  修改商品数量
								goods.number += this.goodsInfo.number
								isExist = true
							}
						})
						
						if (!isExist) {
							// 不存在，存入数组
							goodsList.push(this.goodsInfo)
							// 更新本地存储
							this.setGoodsList(goodsList)
						}
					},
					fail: err => {
						// 存储数据
						let goodsList = [];
						goodsList.push(this.goodsInfo);
						
						this.setGoodsList(goodsList)
					}
				})
			},
			setGoodsList(goodsList) {
				// 存储到本地存储
				uni.setStorage({
					key: 'goodsList',
					data: goodsList,
					success: function () {
						uni.showToast({
							icon: 'success',
							title: '添加购物车成功'
						})
					}
				})
			},
			buy(){ // 立即购买
			
			}
		}
	}
</script>

<style lang="scss">

.status{
	/*  #ifdef  APP-PLUS  */
	opacity: 0;
	/*  #endif  */
}

.swiper-box {
	width: 100%;
	height: 100vw;
	position: relative;
	
	swiper {
		width: 100%;
		height: 100vw;
		
		swiper-item {
			image {
				width: 100%;
				height: 100vw;
			}
		}
	}
	
	.indicator {
		height: 40rpx;
		padding: 0 25rpx;
		position: absolute;
		right: 20rpx;
		bottom: 20rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 22rpx;
		color: #fff;
		background-color: rgba(0, 0, 0, 0.2);
	}
}

.info-box {
	width: 92%;
	padding: 20rpx 4%;
	margin-bottom: 20rpx;
	background-color: #fff;
}

.goods-info {
	.price {
		font-size: 46rpx;
		font-weight: 600;
		color: #f47925;
	}
	.title {
		font-size: 30rpx;
	}
}

.row {
	width: 100%;
	margin: 0 0 30rpx 0;
	display: flex;
	align-items: center;
	
	.text {
		margin-right: 20rpx;
		font-size: 28rpx;
		color: #a2a2a2;
	}
	
	.content {
		font-size: 28rpx;
		display: flex;
		flex-wrap: wrap;
		
		.serviceitem{
			margin-right: 10rpx;
		}
		
		.sp {
			width: 100%;
			display: flex;
			
			view {
				padding: 10rpx;
				margin-right: 10rpx;
				font-size: 20rpx;
				border-radius: 5rpx;
				background-color: #f6f6f6;
				color: #999;
			
				&.on{
					padding: 4rpx 8rpx;
					border: solid 1rpx #f47952;
				}
			}
		}
	}
	
	.arrow {
		position: absolute;
		right: 4%;
		
		.icon {
			color: #ccc;
		}
	}
}

.comments {
	.row {
		width: 100%;
		height: 40rpx;
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
		
		.text {
			font-size: 30rpx;
		}
		
		.arrow {
			position: absolute;
			right: 4%;
			color: #17e6a1;
			font-size: 28rpx;
			
			.show {
				display: flex;
				align-items: center;
				
				.icon {
					color: #17e6a1;
				}
			}
		}
	}
	.comment {
		width: 100%;
		
		.user-info {
			width: 100%;
			height: 40rpx;
			display: flex;
			align-items: center;
			
			.face {
				width: 40rpx;
				height: 40rpx;
				margin-right: 8rpx;
				
				image {
					width: 40rpx;
					height: 40rpx;
					border-radius: 100%;
				}
			}
			
			.username {
				font-size: 24rpx;
				color: #999;
			}
		}
		.content {
			margin-top: 10rpx;
			font-size: 26rpx;
		}
	}
}

.description {
	.title {
		width: 100%;
		height: 80rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 26rpx;
		color: #999;
	}
}

.footer {

	width: 92%;
	height: 99rpx;
	padding: 0 4%;
	display: flex;
	justify-content: space-between;
	align-items: center;
	position: fixed;
	bottom: 0rpx;
	border-top: solid 1rpx #eee;
	background-color: #fff;
	z-index: 2;
	
	.icons {
		height: 80rpx;
		margin-left: -4%;
		display: flex;
		
		.box {
			width: 80rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			flex-wrap: wrap;
			
			.icon {
				font-size: 40rpx;
				color: #828282;
			}
			
			.text {
				width: 100%;
				display: flex;
				justify-content: center;
				font-size: 22rpx;
				color: #666;
			}
		}
	}
	
	.btn {
		height: 80rpx;
		margin-right: -2%;
		display: flex;
		border-radius: 40rpx;
		overflow: hidden;
		
		.joinCart,.buy {
			height: 80rpx;
			padding: 0 40rpx;
			display: flex;
			align-items: center;
			color: #fff;
			font-size: 28rpx;
		}
		
		.joinCart {
			background-color: #f0b46c;
		}
		
		.buy {
			background-color: #f06c7a;
		}
	}
}
</style>
